package test.maven;

import java.util.Arrays;
import java.util.Iterator;
import java.util.List;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.jfinal.plugin.activerecord.ActiveRecordPlugin;
import com.jfinal.plugin.druid.DruidPlugin;

import jodd.http.HttpRequest;
import jodd.http.HttpResponse;

@SuppressWarnings("rawtypes")
public class FetchJobTong {
	
	public static void main(String[] args) {
		
		DruidPlugin dp = new DruidPlugin("jdbc:mysql://192.168.2.10/licai?characterEncoding=UTF-8", "root", "123456");
		ActiveRecordPlugin arp = new ActiveRecordPlugin(dp);
		arp.setDevMode(true);
		arp.setShowSql(true);
		arp.addMapping("lc_job", Job.class);
		dp.start();
		arp.start();
		
		List<Integer> cats = Arrays.asList(new Integer[]{11, 31, 30, 9, 172, 54, 57, 232, 76, 93, 183, 106, 114, 138, 137, 125, 154, 185, 158});
		
		for(int i=1;i<=100;i++) {
			for(Integer catid:cats) {
				String url = "http://www.jobtong.com/api/jobs?category="+catid+"&page="+i;
				System.err.println(url);
				HttpResponse response = HttpRequest.get(url).send();
				String text = response.bodyText();
				JSONObject json = JSON.parseObject(text);
				JSONArray array = json.getJSONArray("items");
				System.err.println("size="+array.size());
				Iterator iter = array.iterator();
				while(iter.hasNext()) {
					JSONObject jobj = (JSONObject)iter.next();
					Integer jobtong_id = jobj.getInteger("id");
					Integer uid = jobj.getInteger("uid");
					Integer company_id = jobj.getInteger("company_id");
					Integer industry_id = jobj.getInteger("industry_id");
					Integer category_id = jobj.getInteger("category_id");
					String name = jobj.getString("name");
					String thread_title = jobj.getString("thread_title");
					String salary = jobj.getString("salary");
					Integer salary_min = jobj.getInteger("salary_min");
					Integer salary_max = jobj.getInteger("salary_max");
					Integer min_degree = jobj.getInteger("min_degree");
					Integer min_experience = jobj.getInteger("min_experience");
					String description = jobj.getString("description");
					Integer city_id = jobj.getInteger("city_id");
					Integer need_count = jobj.getInteger("need_count");
					Long ctime = jobj.getLongValue("ctime");
					Long refresh_time = jobj.getLongValue("refresh_time");
					Long manual_refresh_time = jobj.getLongValue("manual_refresh_time");
					Integer status = jobj.getInteger("status");
					String industry_name = jobj.getString("industry_name");
					String city_name = jobj.getString("city_name");
					String category_name = jobj.getString("category_name");
					String address = jobj.getString("address");
					
					Job job = new Job();
					job.set("jobtong_id", jobtong_id);
					job.set("uid", uid);
					job.set("company_id", company_id);
					job.set("industry_id", industry_id);
					job.set("category_id", category_id);
					job.set("name", name);
					job.set("thread_title", thread_title);
					job.set("salary", salary);
					job.set("salary_min", salary_min);
					job.set("salary_max", salary_max);
					job.set("min_degree", min_degree);
					job.set("min_experience", min_experience);
					job.set("description", description);
					job.set("city_id", city_id);
					job.set("need_count", need_count);
					job.set("ctime", ctime);
					job.set("refresh_time", refresh_time);
					job.set("manual_refresh_time", manual_refresh_time);
					job.set("status", status);
					job.set("industry_name", industry_name);
					job.set("city_name", city_name);
					job.set("category_name", category_name);
					job.set("address", address);
					
					job.save();
		//			JSONObject company = jobj.getJSONObject("company");
		//			Integer c_id = company.getInteger("id");
		//			String c_name = company.getString("name");
		//			String c_alias = company.getString("alias");
		//			Integer c_city_id = company.getInteger("city_id");
		//			Integer c_size = company.getInteger("size");
		//			Integer c_nature = company.getInteger("nature");
		//			String c_logo_url = company.getString("logo_url");
		//			String c_website = company.getString("website");
		//			String c_address = company.getString("address");
		//			Double c_lng = company.getDouble("lng");
		//			Double c_lat = company.getDouble("lat");
		//			Integer c_industry_id = company.getInteger("industry_id");
		//			String c_abstract = company.getString("abstract");
		//			String c_description = company.getString("description");
		//			Integer c_job_count = company.getInteger("job_count");
		//			Long c_ctime = jobj.getLongValue("ctime");
		//			Long c_refresh_time = jobj.getLongValue("refresh_time");
		//			String c_industry_name = jobj.getString("industry_name");
		//			String c_city_name = jobj.getString("city_name");
		//			String c_current_VC_stage = jobj.getString("current_VC_stage");
		//			JSONArray vcs = jobj.getJSONArray("vc_stage");
		//			Iterator vcsiter = vcs.iterator();
		//			while(vcsiter.hasNext()) {
		//				JSONObject vc = (JSONObject) vcsiter.next();
		//				Integer vc_id = vc.getInteger("id");
		//				String vc_stage = vc.getString("stage");
		//				String vc_amount = vc.getString("amount");
		//				String vc_date = vc.getString("date");
		//				String vc_agency = vc.getString("agency");
		//				Long vc_ctime = vc.getLongValue("ctime");
		//				Long vc_mtime = vc.getLongValue("mtime");
		//			}
		//			
		//			JSONObject user = jobj.getJSONObject("user");
		//			Integer u_id = user.getInteger("id");
		//			String u_name = user.getString("name");
		//			Integer u_sex = user.getInteger("sex");
		//			String u_alias = user.getString("alias");
		//			String u_email = user.getString("email");
		//			String u_mobile = user.getString("mobile");
		//			Integer u_company_id = user.getInteger("company_id");
		//			String u_company_name = user.getString("company_name");
		//			String u_company_position = user.getString("company_position");
		//			Integer u_is_company_admin = user.getInteger("is_company_admin");
		//			Integer u_pending_company_id = user.getInteger("pending_company_id");
		//			String u_sign = user.getString("sign");
		//			Long u_ctime = user.getLongValue("ctime");
		//			Integer u_resume_percent = user.getInteger("resume_percent");
		//			String u_experience = user.getString("experience");
		//			String u_degree = user.getString("degree");
		//			String u_photo_url = user.getString("photo_url");
		//			String u_introduction = user.getString("introduction");
		//			String u_birth_day = user.getString("birth_day");
		//			Integer u_work_status = user.getInteger("work_status");
		//			Integer u_current_city_id = user.getInteger("current_city_id");
		//			Integer u_native_city_id = user.getInteger("native_city_id");
		//			Integer u_expect_city_id = user.getInteger("expect_city_id");
		//			Integer u_expect_job_category_id = user.getInteger("expect_job_category_id");
		//			Integer u_expect_min_salary = user.getInteger("expect_min_salary");
		//			Integer u_expect_max_salary = user.getInteger("expect_max_salary");
		//			String u_blog_url = user.getString("blog_url");
		//			String u_sina_weibo_url = user.getString("sina_weibo_url");
		//			String u_github_url = user.getString("github_url");
		//			String u_zcool_url = user.getString("zcool_url");
		//			String u_domain_path = user.getString("domain_path");
		//			String u_current_city_name = user.getString("current_city_name");
		//			String u_native_city_name = user.getString("native_city_name");
		//			String u_expect_city_name = user.getString("expect_city_name");
		//			JSONObject company1 = user.getJSONObject("company");
		//			JSONObject company2 = user.getJSONObject("pending_company");
		//			String u_expect_job_category_name = user.getString("expect_job_category_name");
					
				}
			}
		}
	}

}
